﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using jDepot.Models;
using jDepot.ViewModels;

namespace jDepot.Controllers
{
    public class ProductController : Controller
    {
        private jDepotRepository repository = new jDepotRepository();
        //
        // GET: /Product/

        public ActionResult Index()
        {
            return View("ProductSearchCriteria", new ProductSearchModel(repository.GetAllProductCategories()));
        }

        [HttpPost]
        public ActionResult Search(ProductSearchModel p)
        {
            var searchResult = repository.SearchProduct(p.ProductName, p.ProductCategoryId);

            return View("ProductSearchResult", new ProductSearchModel(searchResult, repository.GetAllProductCategories()));
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Details()
        {
            return View(new ProductViewModel(null, repository.GetAllProductCategories()));
        }

        public ActionResult Add()
        {
            ViewData["ProductCategories"] = new SelectList(repository.GetAllProductCategories(), "ProductCategoryId", "Name");

            return View("ProductDetails", new ProductViewModel(null, repository.GetAllProductCategories()));
        }

        [HttpPost]
        public ActionResult Add(ProductViewModel p)
        {
            if (ModelState.IsValid)
            {
                repository.AddProduct(p.Product);
                repository.Save();
            }
            else
            {
            }

            return View("ProductDetails", new ProductViewModel(p.Product, repository.GetAllProductCategories()));
        }
    }
}
